﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace CommonUnity
{
    /// <summary>
    /// 枚举的扩展功能类
    /// </summary>
    public sealed class EnumHelper
    {
        #region 获取枚举的描述信息

        /// <summary>
        /// 获取枚举的描述信息
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static String GetEnumDesc(Enum e)
        {
            if (e == null)
                return "";

            FieldInfo EnumInfo = e.GetType().GetField(e.ToString());
            if (EnumInfo == null)
                return e.ToString();

            DescriptionAttribute[] EnumAttributes
                = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (EnumAttributes.Length > 0)
            {
                return EnumAttributes[0].Description;
            }
            return e.ToString();

        }

        #endregion

        #region 获取枚举的描述信息

        /// <summary>
        /// 获取枚举的描述信息，根据传入的枚举值
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static String GetEnumDesc(System.Type enumType, string enumValue)
        {
            string result = "";
            foreach (object enumObject in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumObject;
                if (e.ToString() == enumValue)
                {
                    result = GetEnumDesc(e);
                    break;
                }
            }

            return result;

        }

        #endregion

        #region 根据描述信息获取枚举值

        /// <summary>
        /// 根据描述信息获取枚举值
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static Enum GetEnumByDesc(System.Type enumType, string enumValue)
        {
            Enum result = null;
            foreach (object enumObject in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumObject;
                if (GetEnumDesc(e) == enumValue)
                {
                    result = e;
                    break;
                }
            }
            return result;

        }

        #endregion


        public static T ToEnum<T>(int strEnum)
        {
            return (T)Enum.Parse(typeof(T), strEnum.ToString());
        }

        public static T ToEnum<T>(string strEnum)
        {
            return (T)Enum.Parse(typeof(T), strEnum);
        }

        public static int GetEnumValue(System.Type enumType, string txtName)
        {
            string str = Enum.Format(enumType, Enum.Parse(enumType, txtName), "d");
            return Convertor.ConvertToInt(str, 0);
        }

        #region 根据名称获取枚举值
        /// <summary>
        /// 根据名称获取枚举值
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static Enum GetEnumByName(System.Type enumType, string enumName)
        {
            Enum result = null;
            foreach (object enumObject in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumObject;
                if (e.ToString() == enumName)
                {
                    result = e;
                    break;
                }
            }
            return result;
        }

        #endregion

        #region 获取枚举的值字符串信息
        /// <summary>
        /// 获取枚举的值字符串信息，根据传入的枚举值
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static String GetEnumString(Enum e)
        {
            return (Convert.ToInt32(e)).ToString();

        }
        /// <summary>
        /// 获取枚举的值字符串信息，根据传入的枚举值
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static int GetEnumInt(Enum e)
        {
            return Convert.ToInt32(e);

        }
        #endregion

        #region 获取枚举的描述信息
        /// <summary>
        /// 获取枚举的描述信息，根据传入的枚举值
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static String GetEnumDesc(System.Type enumType, int enumValue)
        {
            string result = "";
            foreach (object enumObject in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumObject;
                try
                {
                    if (Convert.ToInt32(e) == enumValue)
                    {
                        result = GetEnumDesc(e);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return result;

        }

        #endregion

        #region 获取枚举的描述信息

        /// <summary>
        /// 获取枚举的描述信息，根据传入的枚举值
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static String GetEnumDesc(System.Type enumType, byte enumValue)
        {
            string result = "";
            foreach (object enumObject in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumObject;
                try
                {
                    if (Convert.ToInt32(e) == enumValue)
                    {
                        result = GetEnumDesc(e);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return result;

        }

        #endregion


        #region 将含有描述信息的枚举绑定到字典中
        /// <summary>
        /// 将含有描述信息的枚举绑定到字典中
        /// </summary>
        /// <param name="listControl"></param>
        /// <param name="enumType"></param>
        public static Dictionary<string, int> GetEnumKeyInt(System.Type enumType)
        {
            Dictionary<string, int> SMPramKey = new Dictionary<string, int>();
            foreach (object enumValue in Enum.GetValues(enumType))
            {
                Enum e = (Enum)enumValue;
                try
                {
                    SMPramKey.Add(GetEnumDesc(e), Convert.ToInt32(enumValue));
                }
                catch (Exception)
                {
                    SMPramKey.Add(GetEnumDesc(e), 0);
                }
            }
            return SMPramKey;
        }
        #endregion

        #region 将含有描述信息的枚举组装datatable
        /// <summary>
        /// 将含有描述信息的枚举组装datatable(Table两个字段，text是描述，value是值(默认是int的值))
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static DataTable GetEnumTable(System.Type enumType)
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add("Text");
            dt.Columns.Add("Value");
            foreach (object enumValue in Enum.GetValues(enumType))
            {
                dr = dt.NewRow();
                Enum e = (Enum)enumValue;
                try
                {
                    dr["Text"] = GetEnumDesc(e);
                    dr["Value"] = Convert.ToInt32(enumValue).ToString();
                    dt.Rows.Add(dr);
                }
                catch (Exception)
                {

                }
            }
            return dt;
        }
        #endregion
    }
}
